package com.cyw.note.manage.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.cyw.common.annotation.RepeatSubmit;
import com.cyw.common.annotation.Log;
import com.cyw.common.core.controller.BaseController;
import com.cyw.common.core.domain.PageQuery;
import com.cyw.common.core.domain.R;
import com.cyw.common.core.validate.AddGroup;
import com.cyw.common.core.validate.EditGroup;
import com.cyw.common.core.validate.QueryGroup;
import com.cyw.common.enums.BusinessType;
import com.cyw.common.utils.poi.ExcelUtil;
import com.cyw.note.manage.domain.vo.MyprojectNoteVo;
import com.cyw.note.manage.domain.bo.MyprojectNoteBo;
import com.cyw.note.manage.service.IMyprojectNoteService;
import com.cyw.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 文章管理Controller
 *
 * @author myproject
 * @date 2022-08-06
 */
@Validated
@Api(value = "文章管理控制器", tags = {"文章管理管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/note")
public class MyprojectNoteController extends BaseController {

    private final IMyprojectNoteService iMyprojectNoteService;

    /**
     * 查询文章管理列表
     */
    @ApiOperation("查询文章管理列表")
    @SaCheckPermission("system:note:list")
    @GetMapping("/list")
    public TableDataInfo<MyprojectNoteVo> list(MyprojectNoteBo bo, PageQuery pageQuery) {
        return iMyprojectNoteService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文章管理列表
     */
    @ApiOperation("导出文章管理列表")
    @SaCheckPermission("system:note:export")
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MyprojectNoteBo bo, HttpServletResponse response) {
        List<MyprojectNoteVo> list = iMyprojectNoteService.queryList(bo);
        ExcelUtil.exportExcel(list, "文章管理", MyprojectNoteVo.class, response);
    }

    /**
     * 获取文章管理详细信息
     */
    @ApiOperation("获取文章管理详细信息")
    @SaCheckPermission("system:note:query")
    @GetMapping("/{id}")
    public R<MyprojectNoteVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(iMyprojectNoteService.queryById(id));
    }

    /**
     * 新增文章管理
     */
    @ApiOperation("新增文章管理")
    @SaCheckPermission("system:note:add")
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MyprojectNoteBo bo) {
        return toAjax(iMyprojectNoteService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改文章管理
     */
    @ApiOperation("修改文章管理")
    @SaCheckPermission("system:note:edit")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MyprojectNoteBo bo) {
        return toAjax(iMyprojectNoteService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除文章管理
     */
    @ApiOperation("删除文章管理")
    @SaCheckPermission("system:note:remove")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable String[] ids) {
        return toAjax(iMyprojectNoteService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
